﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Core.Extensions;
using App.Domain;
using App.Web.ViewModels;
using App.Web.Utility;
using AutoMapper;
using App.Core.Xml;
using App.Web.ViewModels.UtilityModels;

namespace App.Web.Controllers
{
    public class SearchController : BaseController
    {
		private AppEntities _Context = new AppEntities();
        
		#region Constructors
		public SearchController()
		{
		}

		public SearchController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods

        #region Obsolete Code
        /// <summary>
        /// Obsolete Method. Now the search function is in Product controller.
        /// And Commenting it as it is hiding a method of base Controller class of ASP .NET MVC
        /// </summary>
        /// <param name="id"></param>
        /// <param name="SearchKey"></param>
        /// <returns></returns>
        //public ActionResult View(String id , String SearchKey = "")
        //{
            
        //    int pageNo = id.ToInt() > 0 ? id.ToInt() : 1;
        //    int pageSize = ConfigReader.pageSize;

        //    UtilityDAO dao = new UtilityDAO();
        //    DbParameter[] parameters = new[] { 
        //    new DbParameter("@CompanyID", 1), 
        //    new DbParameter("@SearchKey", SearchKey),
        //    new DbParameter("@UserID",(SessionCache.CurrentUser==null)? 0 : SessionCache.CurrentUser.ID),
        //    };
        //    int totalRecord = 0;
        //    System.Data.DataSet ds =
        //        dao.GetPagedData((SearchKey=="Pupular Search")?DbObject.SP.GET_PRODUCT_POPULAR_SEARCH:
        //        (SearchKey=="Discount Products")?DbObject.SP.GET_PRODUCT_BY_DISCOUNT_PRODUCTS:
        //        (SearchKey == "Special Offer") ? DbObject.SP.GET_PRODUCT_BY_SPECIAL_OFFERS : 
        //        DbObject.SP.GET_PRODUCT_BY_SEARCH
        //        ,
        //        parameters, pageNo, pageSize, out totalRecord, true);
        //    ViewBag.PageNumber = pageNo;
        //    ViewBag.PageSize = pageSize;
        //    ViewBag.TotalRecord = totalRecord;
        //    ViewBag.DataSource = ds;
        //    ViewBag.SearchType = SearchKey;
        //    //ViewBag.ShowEditLink = true;

        //    ViewModels.UtilityModels.PagerConfig pagerConfig = new ViewModels.UtilityModels.PagerConfig();
        //    pagerConfig.PageNo = pageNo;
        //    pagerConfig.PageSize = pageSize;
        //    //pagerConfig.TotalRecordMessage = "Total {0} Products Found";


        //    pagerConfig.UrlFormat = (SearchKey == "Pupular Search") ? "/Search/View?id={0}&SearchKey=Pupular Search" :
        //        (SearchKey == "Discount Products") ? "/Search/View?id={0}&SearchKey=Discount Products" :
        //        (SearchKey == "Special Offer") ? "/Search/View?id={0}&SearchKey=Special Offer" :
        //        "/Search/View?id={0}";

        //    ViewBag.PagerConfig = pagerConfig;
        //    ViewBag.TotalRecord = pagerConfig.TotalRecord = totalRecord;

        //    return View();
        //}

        #endregion Obsolete Code

        /// <summary>
        /// Builds Filter Html from a Category Definition ZGL
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public PartialViewResult BuildFilters(int categoryID = 0)
        {
            App.Web.ViewModels.ZGL.Category.Category categoryZGL = null;
            Category category = _Context.Categories.FirstOrDefault(P => P.ID == categoryID);
            if (category != null && !category.PropertyDefinition.IsEmpty())
            {
                categoryZGL = XmlHelper.ToObject<App.Web.ViewModels.ZGL.Category.Category>(category.PropertyDefinition);                
            }
            return PartialView("_SearchFilters", categoryZGL);
        }

        [HttpPost]
        public PartialViewResult RefineSearch(String json)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            App.Search.SearchPayload payload = serializer.Deserialize<App.Search.SearchPayload>(json);

            PagerConfig pagerConfig = new PagerConfig(1, 10, 0);
            int totalRecord = 0;
            ///Search in Lucene Index
            App.Search.ProductSearchManager searcher = new Search.ProductSearchManager();            
            String productIDs = searcher.RefineSearch(payload, pagerConfig.PageSize, pagerConfig.PageNo, out totalRecord);            
            pagerConfig.TotalRecord = totalRecord;

            ///Get the Resulting Product Information from Database
            UtilityDAO dao = new UtilityDAO();
            DbParameter[] parameters = new[] { 
                new DbParameter("@ProductIDs", productIDs),                 
                new DbParameter("@UserID", SessionCache.CurrentUser == null ? 0 : SessionCache.CurrentUser.ID)
            };
            System.Data.DataSet ds = dao.GetDataSet(DbObject.SP.GET_PRODUCTS_BY_ID, parameters, true);
            ListConfig listConfig = new ListConfig(ds.Tables[0], pagerConfig);
            ViewBag.SearchType = payload.Keyword;
            return PartialView("_AjaxSearchResult", listConfig);
        }
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
